<?php

namespace app\admin\controller;

use app\BaseController;

class Student extends BaseController
{
    /**
     * 2025 03 04
     * Administrator
     * @return \think\response\Json
     */
    public function classes()
    {

        try {
            $classesObj = new \app\common\business\Classes();
            $result = $classesObj->classes();
            return show(config('common.http_status.success'), 'ok', $result);
        } catch (Exception $e) {
            return show($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 2025 03 04
     * Administrator
     * @return \think\response\Json
     */
    public function add_classes()
    {
        $name = $this->request->param('name', '', 'trim');
        $start_date = $this->request->param('start_date', '', 'trim');
        $week = $this->request->param('week', '', 'trim');
        $subject_id = $this->request->param('subject_id', '', 'trim');
        $data = [
            'name' => $name,
            'start_date' => ((int)$start_date),
            'week' => $week,
            'subject_id' => $subject_id,
        ];

        try {
            $classObj = new \app\common\business\Classes();
            $result = $classObj->add_classes($data);
            return show(config('common.http_status.success'), 'ok', $result);
        } catch (Exception $e) {
            return show($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 2025 03 04
     * Administrator
     * @return \think\response\Json
     */
    public function edit_classes()
    {
        $id = $this->request->param('id', '', 'trim');
        $name = $this->request->param('name', '', 'trim');
        $start_date = $this->request->param('start_date', '', 'trim');
        $week = $this->request->param('week', '', 'trim');
        $subject_id = $this->request->param('subject_id', '', 'trim');
        $data = [
            'id' => $id,
            'name' => $name,
            'start_date' => $start_date,
            'week' => $week,
            'subject_id' => $subject_id,
        ];

        try {
            $classObj = new \app\common\business\Classes();
            $result = $classObj->edit_classes($data);
            return show(config('common.http_status.success'), 'ok', $result);
        } catch (Exception $e) {
            return show($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 2025 03 04
     * Administrator
     * @return \think\response\Json
     */
    public function del_classes()
    {
        $id = $this->request->param('id', '', 'trim');
        try {
            $classObj = new \app\common\business\Classes();
            $result = $classObj->del_classes($id);
            return show(config('common.http_status.success'), 'ok', $result);
        } catch (Exception $e) {
            return show($e->getCode(), $e->getMessage());
        }
    }


    /**
     * 2025 03 04
     * Administrator
     * @return \think\response\Json
     */
    public function student()
    {

        $list_rows = $this->request->param('list_rows', '10', 'trim');
        $page = $this->request->param('page', '1', 'trim');
        $classes_id = $this->request->param('classes_id', '', 'trim');
        $today_sign_in = $this->request->param('today_sign_in', '', 'trim');

        $data = [
            'classes_id' => $classes_id,
            'list_rows' => $list_rows,
            'page' => $page,
            'today_sign_in' => $today_sign_in,
        ];
        $data = array_filter($data);

        try {
            $studentObj = new \app\common\business\Student();
            $result = $studentObj->student($data);
            return show(config('common.http_status.success'), 'ok', $result);
        } catch (Exception $e) {
            return show($e->getCode(), $e->getMessage());
        }
    }


    /**
     * 2025 03 04
     * Administrator
     * @return \think\response\Json
     */
    public function add_student()
    {
        $name = $this->request->param('name', '', 'trim');
        $avatar = $this->request->param('avatar', '', 'trim');
        $phone = $this->request->param('phone', '', 'trim');
        $classes_id = $this->request->param('classes_id', '', 'trim');
        $renew_date = $this->request->param('renew_date', 0, 'intval');
        $data = [
            'name' => $name,
            'avatar' => $avatar,
            'phone' => $phone,
            'classes_id' => $classes_id,
            'renew_date' => $renew_date/1000,
        ];

        try {
            $studentObj = new \app\common\business\Student();
            $result = $studentObj->add_student($data);
            return show(config('common.http_status.success'), 'ok', $result);
        } catch (Exception $e) {
            return show($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 2025 03 04
     * Administrator
     * @return \think\response\Json
     */
    public function del_student()
    {
        $id = $this->request->param('id', '', 'trim');
        try {
            $studentObj = new \app\common\business\Student();
            $result = $studentObj->del_student($id);
            return show(config('common.http_status.success'), 'ok', $result);
        } catch (Exception $e) {
            return show($e->getCode(), $e->getMessage());
        }
    }


    /**
     * 2025 03 04
     * Administrator
     * @return \think\response\Json
     */
    public function edit_student()
    {
        $id = $this->request->param('id', '', 'trim');
        $name = $this->request->param('name', '', 'trim');
        $avatar = $this->request->param('avatar', '', 'trim');
        $phone = $this->request->param('phone', '', 'trim');
        $classes_id = $this->request->param('classes_id', '', 'trim');
        $renew_date = $this->request->param('renew_date', 0, 'intval');
        $score = $this->request->param('score', 0, 'intval');
        $data = [
            'id' => $id,
            'name' => $name,
            'avatar' => $avatar,
            'phone' => $phone,
            'classes_id' => $classes_id,
            'renew_date' => $renew_date/1000,//毫秒转换成秒
            'score' => $score,
        ];

        try {
            $studentObj = new \app\common\business\Student();
            $result = $studentObj->edit_student($data);
            return show(config('common.http_status.success'), 'ok', $result);
        } catch (Exception $e) {
            return show($e->getCode(), $e->getMessage());
        }
    }


//    删除用户
    public function sign_in_student()
    {

        $ids = $this->request->param('ids', []);
        $re_sign_in_date = $this->request->param('re_sign_in_date', 0, 'intval');
        if (empty($ids)) {
            return show(config('status.error'), '数据为空', []);
        }
        try {
            $studentObj = new \app\common\business\Student();
            $result = $studentObj->sign_in_student($ids,$re_sign_in_date);
            return show(config('common.http_status.success'), 'ok', $result);
        } catch (Exception $e) {
            return show($e->getCode(), $e->getMessage());
        }
    }

}